/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.util.ThemeManager;
import com.mol.market.common.widget.BaseActivity;
import com.mol.market.common.widget.GalleryEx;
import com.mol.market.common.widget.GalleryEx.OnScreenChangeListener;


/**
 * @author andrew.wang
 * @date   2012-2-21
 * @since  Version 0.9.2
 *
 */
public class UserGuideActivity extends BaseActivity implements OnClickListener {

    /** 背景图 */
    private final static int[] BACKGROUND_ARRAY = {
        R.drawable.step_1, R.drawable.step_2, R.drawable.step_3,
        R.drawable.step_4, R.drawable.step_5, R.drawable.step_6
    };
    
    /** 描述信息 */
    private final static int[] INFO_ARRAY = {
        R.drawable.info_1, R.drawable.info_2, R.drawable.info_3,
        R.drawable.info_4, R.drawable.info_5, R.drawable.info_6
    };
    
    private GalleryEx mGallery;
    private boolean mIsFinalPage;
    private boolean mIsOnlyView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_user_guide_layout);
        
        final ImageView info = (ImageView) findViewById(R.id.step_description);
        final ProgressBar progress = (ProgressBar) findViewById(R.id.step);
        progress.setProgress(16);
        final ImageView dark = (ImageView) findViewById(R.id.theme_dark);
        dark.setOnClickListener(this);
        final ImageView light = (ImageView) findViewById(R.id.theme_light);
        light.setOnClickListener(this);
        final TextView next = (TextView) findViewById(R.id.next);
        next.setOnClickListener(this);
        
        mIsOnlyView = getIntent().getBooleanExtra(Constants.EXTRA_ACTION_VIEW, false);
        
        mGallery = (GalleryEx) findViewById(R.id.guide_gallery);
        mGallery.setOnScreenChangeListener(new OnScreenChangeListener() {
            @Override
            public void screenChange(int currentTab) {
                info.setImageResource(INFO_ARRAY[currentTab]);
                int pg = 0;
                if (currentTab == 5) {
                    dark.setVisibility(View.VISIBLE);
                    light.setVisibility(View.VISIBLE);
                    next.setText(R.string.start);
                    next.setTextAppearance(getApplicationContext(), R.style.guide_finish_text);
                    mIsFinalPage = true;
                    pg = 100;
                } else {
                    dark.setVisibility(View.GONE);
                    light.setVisibility(View.GONE);
                    next.setText(R.string.next_step);
                    next.setTextAppearance(getApplicationContext(), R.style.text_style_3e);
                    mIsFinalPage = false;
                    pg = 16 * (currentTab + 1);
                }
                progress.setProgress(pg);
            }
            
            @Override
            public void moveToNext() {
                // 进首页
                gotoHome();
            }
        });
        
        LayoutInflater layoutInflater = LayoutInflater.from(getApplicationContext());
        
        for (int i = 0; i < 6; i++) {
            ImageView v = (ImageView) layoutInflater.inflate(R.layout.user_guide_gallery_item,
                    mGallery, false);
            v.setImageResource(BACKGROUND_ARRAY[i]);
            mGallery.addView(v);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();

    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        int count = mGallery.getChildCount();
        for (int i = 0; i < count; i++) {
            ImageView v = (ImageView) mGallery.getChildAt(i);
            Drawable d = v.getDrawable();
            v.setImageDrawable(null);
            if (d != null) {
                d.setCallback(null);
            }
        }
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 禁止返回操作
            if (mGallery != null) {
                mGallery.previousPage();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.theme_dark:
            
            mSession.setTheme(ThemeManager.THEME_TYPE_DARK);
            sendBroadcast(new Intent(Constants.BROADCAST_CHANGE_THEME));
            gotoHome();
            break;
            
        case R.id.theme_light:
            
            mSession.setTheme(ThemeManager.THEME_TYPE_LIGHT);
            sendBroadcast(new Intent(Constants.BROADCAST_CHANGE_THEME));
            gotoHome();
            break;
            
        case R.id.next:
            
            if (mIsFinalPage) {
                gotoHome();
                return;
            }
            mGallery.nextPage();
            break;

        default:
            break;
        }
    }
    
    /*
     * 去首页
     */
    private void gotoHome() {
        Intent i = getIntent();
        i.setClass(getApplicationContext(), HomeTabActivity.class);
        if (!mIsOnlyView) {
            startActivity(i);
        }
        finish();
    }
}
